// pages/orderdetail/orderdetail.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        primary: app.globalData.primary, // 主题色
        ossPath: app.globalData.ossPath,
        orderId: null, // 订单号
        orderDetail: {}, //订单详情
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //设置主题色
        if (app.globalData.primary) {
            this.setData({ primary: app.globalData.primary })
            app.usePrimary(app.globalData.primary)
        }

        this.setData({orderId: options.n})
        this.getOrderDetail();
    },
    getOrderDetail: function() { // 查询订单详情
        let url = '/order?Id=' + this.data.orderId;
        let that = this;
        wx.request({
            url: app.globalData.serverPath + url,
            header: app.globalData.header,
            success: function (res) {
                var data = that.filterData(res.data);
                that.setData({ orderDetail: data})
            }
        })
    },
    filterData: function (orderDetail) {
        orderDetail.CreatedDate = orderDetail.CreatedDate.replace('T', ' ')
        orderDetail.ProductTotal = parseFloat(orderDetail.ProductTotal).toFixed(2)
        orderDetail.FreightActual = parseFloat(orderDetail.FreightActual).toFixed(2)
        orderDetail.CouponAmount = (orderDetail.CouponAmount && parseFloat(orderDetail.CouponAmount).toFixed(2)) || '0.00'
        orderDetail.Amount = parseFloat(orderDetail.Amount).toFixed(2)
        orderDetail.OrderItems.forEach(item => {
            var src = '';
            if (item.FilePath.indexOf('.png') > -1) {
                src = this.data.ossPath + item.FilePath.replace(/^(http|https):\/\/img.alinyin.com/, '')
            }
            if (!src && item.ThumbnailsUrl) {
                src = this.data.ossPath + item.ThumbnailsUrl.replace(/^(http|https):\/\/img\.aliyin\.com/, '')
            }
            item.src = src;
            
            item.Amount = parseFloat(item.Amount).toFixed(2)
        })
        return orderDetail
    },
    handleCancel(e) {  // 取消订单
        var order = e.currentTarget.dataset.order;
        var index = e.currentTarget.dataset.index;
        wx.showModal({
            title: '确认取消订单吗？',
            success: (res) => {
                if (res.confirm) {
                    let data = `OrderCode=${order}`
                    // console.log('用户点击确定', data)
                    wx.request({
                        url: app.globalData.serverPath + '/Orders',
                        method: 'put',
                        header: app.globalData.header,
                        data: data,
                        success: ({ data }) => {
                            if (data.state == 'ok') {
                                wx.showToast({
                                    title: '取消成功',
                                })
                                wx.navigateBack({
                                    delta: 1
                                })
                            } else {
                                wx.showToast({
                                    title: '取消失败',
                                })
                            }
                        },
                        fail: err => {
                            wx.showToast({
                                title: '取消失败',
                            })
                        }
                    })
                }
            }
        })
    },
    gotoPayment: function (e) { // 去支付
        var orderCode = e.currentTarget.dataset.order;
        wx.navigateTo({
            url: '../payment/index?orderCode=' + orderCode,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})